﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NObsidian;
using NObsidian.Containers;
using System.IO;
using System.Reflection;
using System.Configuration;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private NObsidian.ObsidianProxy proxy;
        private string ConsumerKey;
        private string ConsumerSecret;

        public Form1()
        {
            InitializeComponent();

            SetUpCombos();
            ReadConfigFile();


            proxy = new NObsidian.ObsidianProxy(ConsumerKey, ConsumerSecret);
        }

        private void ReadConfigFile()
        {
            ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"];
            ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"];

        }

        private void SetUpCombos()
        {
            cbCharField.DataSource = ContainerBase.GetContainerPropertyList(typeof(Character));
            cbWikiField.DataSource = ContainerBase.GetContainerPropertyList(typeof(WikiPage));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string url;

                url = proxy.FirstLoginStep();
                System.Diagnostics.Process.Start(url);

                MessageBox.Show("Please, login and enter the PIN code in the text box");
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message + "\n" + ex.StackTrace;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtPIN.Text))
                {
                    MessageBox.Show("Invalid PIN");
                }
                else
                {
                    proxy.SecondLoginStep(txtPIN.Text);
                    txtOutput.Text = "Login Completed";
                }
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message + "\n" + ex.StackTrace;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                Campaign c = proxy.CampaignManager.GetCampaign(txtCampaignId.Text);

                txtOutput.Text = c.ToString();
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message + "\n" + ex.StackTrace;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                User u = proxy.UserManager.ShowMe();

                txtOutput.Text = u.ToString();
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message + "\n" + ex.StackTrace;
            }
        }

          private void btGetChars_Click(object sender, EventArgs e)
        {
            StringBuilder sb;

            try
            {
                List<Character> u = proxy.CharacterManager.GetCampaignCharacterIndex(txtCampaignIdChar.Text);

                sb = new StringBuilder();
                foreach (Character c in u)
                {
                    sb.AppendLine(c.ToString());
                }

                txtOutput.Text = sb.ToString();
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message + "\n" + ex.StackTrace;
            }


        }

        private void btGetChar_Click(object sender, EventArgs e)
        {
            try
            {
                Character c = proxy.CharacterManager.GetCharacter(txtCampaignIdChar.Text, txtCharacterId.Text);
                                
                txtOutput.Text = c.ToString();
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message + "\n" + ex.StackTrace;
            }
        }

        private void btGetWikiPages_Click(object sender, EventArgs e)
        {
            StringBuilder sb;
            try
            {
                List<WikiPage> w = proxy.WikiPageManager.GetCampaignWikiPagesIndex(txtCampaignIdWiki.Text);

                sb = new StringBuilder();
                foreach (WikiPage c in w)
                {
                    sb.AppendLine(c.ToString());
                }

                txtOutput.Text = sb.ToString();
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message + "\n" + ex.StackTrace;
            }
        }

        private void btGetWiki_Click(object sender, EventArgs e)
        {
            try
            {
                WikiPage w = proxy.WikiPageManager.GetCampaignWikiPage(txtCampaignIdWiki.Text, txtWikiPageId.Text);

                txtOutput.Text = w.ToString();
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message + "\n" + ex.StackTrace;
            }
        }

        private void btShowUser_Click(object sender, EventArgs e)
        {
            try
            {
                User u = proxy.UserManager.ShowUser(txtUserId.Text);

                txtOutput.Text = u.ToString();
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message + "\n" + ex.StackTrace;
            }
        }

        private void btUpdateChar_Click(object sender, EventArgs e)
        {
            try
            {
                Character c = proxy.CharacterManager.GetCharacter(txtCampaignIdChar.Text, txtCharacterId.Text);
                txtOutput.Text = c.ToString();

                SetValue(c, cbCharField.Text, txtCharValue.Text);

                c = proxy.CharacterManager.UpdateCharacter(txtCampaignIdChar.Text, txtCharacterId.Text, c);

                txtOutput.Text = proxy.GetJSONFromObject<Character>(c);
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message + "\n" + ex.StackTrace;
            }
        }

        private void btUpdateWiki_Click(object sender, EventArgs e)
        {
            try
            {
                WikiPage w = proxy.WikiPageManager.GetCampaignWikiPage(txtCampaignIdWiki.Text, txtWikiPageId.Text);
                txtOutput.Text = w.ToString();

                SetValue(w, cbWikiField.Text, txtWikiValue.Text);

                w = proxy.WikiPageManager.UpdateCampaignWikiPage(txtCampaignIdWiki.Text, txtWikiPageId.Text, w);
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message + "\n" + ex.StackTrace;
            }
        }

        private void SetValue(object o, string propertyName, string propertyValue)
        {
            PropertyInfo p = o.GetType().GetProperty(propertyName);
            p.SetValue(o, propertyValue, null);
        }

        private void btDeleteChar_Click(object sender, EventArgs e)
        {
            try
            {
                Character c = proxy.CharacterManager.GetCharacter(txtCampaignIdChar.Text, txtCharacterId.Text);

                if (MessageBox.Show(string.Format("Are you sure you want to delete character '{0}'?", c.Name),
                    "Obsidian Proxy Test tool", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    proxy.CharacterManager.DeleteCharacter(txtCampaignIdChar.Text, txtCharacterId.Text);
                }
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message + "\n" + ex.StackTrace;
            }
        }

        private void btDeleteWiki_Click(object sender, EventArgs e)
        {
            try
            {
                WikiPage w = proxy.WikiPageManager.GetCampaignWikiPage(txtCampaignIdWiki.Text, txtWikiPageId.Text);

                if (MessageBox.Show(string.Format("Are you sure you want to delete wiki page '{0}'?", w.Name),
                    "Obsidian Proxy Test tool", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    proxy.WikiPageManager.DeleteCampaignWikiPage(txtCampaignIdWiki.Text, txtWikiPageId.Text);
                }
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message + "\n" + ex.StackTrace;
            }
        }

        private void btCreateChar_Click(object sender, EventArgs e)
        {
            try
            {
                Character cNew, c;

                c = proxy.CharacterManager.GetCharacter(txtCampaignIdChar.Text, txtCharacterId.Text);

                //cNew = c.GetCopy() as Character;
                //cNew.Name = cNew.Name + " ..Copy..";

                cNew = new Character();
                cNew.Name = "Prueba";
                cNew.TagLine = "Test TagLine";
                cNew.Description = "DEsc Prueba";
                cNew.Bio = "Bioooorl";
                cNew.GameMasterInfo = "prueba";
                cNew.CampaignId = c.CampaignId;

                cNew.Campaign = c.Campaign;

                cNew.Author = new User();
                cNew.Author.Id = c.Author.Id;
                cNew.Author.UserName = c.Author.UserName;
                cNew.Author.Slug = c.Author.Slug;
                cNew.Author.ProfileUrl = c.Author.ProfileUrl;

                cNew.AuthorId = c.Author.Id;

                c = proxy.CharacterManager.CreateCharacter(txtCampaignIdChar.Text, cNew);

                txtOutput.Text = proxy.GetJSONFromObject<Character>(c);
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message;
            }
        }

        private void btCreateWiki_Click(object sender, EventArgs e)
        {
            try
            {
                WikiPage wNew, w;

                wNew = new WikiPage();
                wNew.Name = "Probando2";

                w = proxy.WikiPageManager.CreateCampaignWikiPage(txtCampaignIdWiki.Text, wNew);
                txtOutput.Text = proxy.GetJSONFromObject<WikiPage>(w);
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message + "\n" + ex.StackTrace;
            }
        }

    }
}
